package ren.xbcr.schoolbus.client;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Build;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

import ren.xbcr.schoolbus.client.util.Constants;
import ren.xbcr.schoolbus.client.util.Tool;

public class BusinessCooperationActivity extends AppCompatActivity {

    private TextView businessCooperationViewer;



    private Button mCallCarButton;
    private Button mDriverRatingButton;
    private Button mAboutBusinessButton;
    private Button mRecruitDriverButton;
    private View mProgressView;
    private WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_business_cooperation);

        mProgressView = findViewById(R.id.business_progress);


        myWebView = (WebView) findViewById(R.id.business_cooperation_webview);
        myWebView.loadUrl(Constants.BUSINESS_COOPERATION_WEB_RUL);
        myWebView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                showProgress(false);
            }
        });

/*        businessCooperationViewer = (TextView) findViewById(R.id.business_cooperation_view);
        Resources res = getResources();
        String text = res.getString(R.string.business_cooperation_styled_description);*/
//        CharSequence styledText = Html.fromHtml(text);
//        businessCooperationViewer.setText(styledText);
//        businessCooperationViewer.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p><span style=\"color:#FF0000;\">editor</span> has a larger"));



        showProgress(true);
        setupBtn();
    }



    private void setupBtn() {
        final FragmentActivity activity = this;
        mCallCarButton = (Button) findViewById(R.id.main_map_button);
        mCallCarButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Tool.navToLoginActivity(BusinessCooperationActivity.this, activity)) return;
                Intent intent = new Intent(BusinessCooperationActivity.this, MainActivity.class);
                intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY);
                startActivity(intent);
            }
        });

        mDriverRatingButton = (Button) findViewById(R.id.driver_rating_btn);
        mDriverRatingButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Tool.navToLoginActivity(BusinessCooperationActivity.this, activity)) return;
                Intent intent = new Intent(BusinessCooperationActivity.this, DriverListActivity.class);
                intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY);
                startActivity(intent);
            }
        });

        mAboutBusinessButton = (Button) findViewById(R.id.about_business_btn);
        mAboutBusinessButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*if (Tool.navToLoginActivity(BusinessCooperationActivity.this, activity)) return;
                Intent intent = new Intent(BusinessCooperationActivity.this, BusinessCooperationActivity.class);
                intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY);
                startActivity(intent);*/
            }
        });

        mRecruitDriverButton = (Button) findViewById(R.id.recruit_driver_btn);
        mRecruitDriverButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Tool.navToLoginActivity(BusinessCooperationActivity.this, activity)) return;
                Intent intent = new Intent(BusinessCooperationActivity.this, RecruitDriverActivity.class);
                intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY);
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // app icon in action bar clicked; goto parent activity.
                this.finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }




    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
    private void showProgress(final boolean show) {
        // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
        // for very easy animations. If available, use these APIs to fade-in
        // the progress spinner.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);

            myWebView.setVisibility(show ? View.GONE : View.VISIBLE);
            myWebView.animate().setDuration(shortAnimTime).alpha(
                    show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    myWebView.setVisibility(show ? View.GONE : View.VISIBLE);
                }
            });

            mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
            mProgressView.animate().setDuration(shortAnimTime).alpha(
                    show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
                }
            });
        } else {
            // The ViewPropertyAnimator APIs are not available, so simply show
            // and hide the relevant UI components.
            mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
            myWebView.setVisibility(show ? View.GONE : View.VISIBLE);
        }
    }
}
